package cn.youqu.happy.module.youqu.controller.app.userComment;

import cn.hutool.core.lang.tree.Tree;
import cn.youqu.happy.framework.common.pojo.CommonResult;
import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.security.core.annotations.PreAuthenticated;
import cn.youqu.happy.module.youqu.controller.admin.userComment.vo.UserCommentPageReqVO;
import cn.youqu.happy.module.youqu.controller.app.userComment.vo.*;
import cn.youqu.happy.module.youqu.convert.userComment.UserCommentConvert;
import cn.youqu.happy.module.youqu.dal.dataobject.userComment.UserCommentDO;
import cn.youqu.happy.module.youqu.service.userComment.UserCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.youqu.happy.framework.common.pojo.CommonResult.success;
import static cn.youqu.happy.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "用户 APP - 用户回复")
@RestController
@RequestMapping("/youqu/user-comment")
@Validated
public class AppUserCommentController {
    /**
     * get list page 均只能获取自己发布的评论
     * TODO 1.获取帖子评论(加楼主回复）
     * TODO 2.获取回复评论 （展开回复）
     * TODO 3.获取自己的评论记录
     * TODO 4.获取他人的评论记录
     */
    @Resource
    private UserCommentService userCommentService;

    @PostMapping("/publish")
    @ApiOperation("发布用户评论")
    @PreAuthenticated
    public CommonResult<Integer> publishUserComment(@Valid @RequestBody AppUserCommentPublishReqVO createReqVO) {
        Long loginUserId = getLoginUserId();
        return success(userCommentService.publishUserComment(createReqVO, loginUserId));
    }

    @PostMapping("/reply-assess")
    @ApiOperation("用户回复评价")
    @PreAuthenticated
    public CommonResult<Integer> replyAssessUserComment(@Valid @RequestBody AppUserCommentReplyAssessReqVO replyAssessReqVO) {
        Long loginUserId = getLoginUserId();
        return success(userCommentService.replyAssessUserComment(replyAssessReqVO, loginUserId));
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户回复")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<Boolean> deleteUserComment(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();
        userCommentService.deleteUserComment(id, loginUserId);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户回复")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<AppUserCommentRespVO> getUserComment(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();
        UserCommentDO userComment = userCommentService.getUserComment(id, loginUserId);
        return success(UserCommentConvert.INSTANCE.convert0(userComment));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户回复列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthenticated

    public CommonResult<List<AppUserCommentRespVO>> getUserCommentList(@RequestParam("ids") Collection<Integer> ids) {
        Long loginUserId = getLoginUserId();
        List<UserCommentDO> list = userCommentService.getUserCommentList(ids, loginUserId);
        return success(UserCommentConvert.INSTANCE.convertList0(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户回复分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppUserCommentRespVO>> getUserCommentPage(@Valid AppUserCommentPageReqVO pageVO) {
        Long loginUserId = getLoginUserId();
        UserCommentPageReqVO convert = UserCommentConvert.INSTANCE.convert(pageVO);
        PageResult<UserCommentDO> pageResult = userCommentService.getUserCommentPage(convert, loginUserId);
        return success(UserCommentConvert.INSTANCE.convertPage0(pageResult));
    }

    @GetMapping("/comment-detail-page")
    @ApiOperation("获得用户说说评论详情分页")
    @PreAuthenticated
    public CommonResult<PageResult<Tree<AppUserCommentDetailRespVO>>> getUserCommentPage(@Valid AppUserCommentDetailPageReqVO pageVO) {
        Long loginUserId = getLoginUserId();
        return success(userCommentService.getUserCommentDetailPageByTellId(pageVO, loginUserId));
    }


}